package reduce.fz.com.reduce.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 15/7/21.
 */
public class DoorBtns extends LinearLayout {

    private List<DoorBtn> doorBtns = new ArrayList<DoorBtn>();
    private DoorInterface doorInterface;
    private DoorBtn curretBtn;
    private DoorBtn clickBtn;
    private DoorBtn temp;

    private int weight;

    public void setWeight(int weight) {
        this.weight = weight;
    }



    public DoorBtns(Context context) {
        super(context);
    }

    public DoorBtns(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addDoorBtn(DoorBtn btn) {
        btn.setIndex(doorBtns.size() + 1);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(curretBtn==(DoorBtn)view) return;
                changeBtn((DoorBtn) view);
                doorInterface.onSelected( ((DoorBtn) view).getIndex());
            }
        });
        LinearLayout.LayoutParams ly=new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT);

        ly.weight=1;
        btn.setLayoutParams(ly);

        this.addView(btn);
        doorBtns.add(btn);
    }

    public void changeBtn(DoorBtn btn) {
        if(curretBtn!=null) {
            curretBtn.setUnSelect();
        }
        btn.setSelect();
        curretBtn = btn;

    }

    public static  interface DoorInterface {

        public int onSelected(int i);
    }

    public void setDoorInterface(DoorInterface doorInterface) {
        this.doorInterface = doorInterface;
    }
}
